package com.analyse.stock.strategy.indicator;

import org.ta4j.core.BarSeries;
import org.ta4j.core.Rule;
import org.ta4j.core.TradingRecord;
import org.ta4j.core.indicators.SMAIndicator;
import org.ta4j.core.indicators.helpers.VolumeIndicator;
import org.ta4j.core.num.Num;

public class VolumeSMAIndicator implements Rule {
    private final BarSeries series;
    private final SMAIndicator volumeSMA; // 3 日成交量均值指标

    public VolumeSMAIndicator(BarSeries series, int smaPeriod) {
        this.series = series;
        this.volumeSMA = new SMAIndicator(new VolumeIndicator(series), smaPeriod);
    }

    @Override
    public boolean isSatisfied(int index) {
        if (index < 2) {
            return false; // 数据不足以计算 3 日均值
        }

        // 当前成交量
        Num currentVolume = series.getBar(index).getVolume();
        // 3 日成交量均值
        Num averageVolume = volumeSMA.getValue(index);

        // 当前 K 线的开盘价和收盘价
        Num openPrice = series.getBar(index).getOpenPrice();
        Num closePrice = series.getBar(index).getClosePrice();

        // 检查条件：成交量 > 3 日均值 * 2，且收红
        boolean volumeCondition = currentVolume.isGreaterThan(averageVolume.multipliedBy(series.numOf(2)));
        boolean bullishCondition = closePrice.isGreaterThan(openPrice);

        // 两个条件同时满足
        return volumeCondition && bullishCondition;
    }

    @Override
    public boolean isSatisfied(int i, TradingRecord tradingRecord) {
        return false;
    }
}